#!/bin/bash
# Usage: compdf [LIST OF INPUT FILES].
# The purpose is to combine the input files into a pdf file.
# The output will be written to output-\`date +%s\`.pdf."

USAGE="\nUsage: compdf LIST OF INPUT FILES [-o|--output OUTPUTFILE].\n"

# Parse the arguments
INPUTFILE=""
OUTPUTFILE=""
while [[ -n "$1" ]]; do
	case "$1" in
    -o|--output)
        if [[ $# -gt 1 ]]; then
            OUTPUTFILE="$2"
            shift
        else
            printf "%s" "$USAGE"
            exit 1
        fi
        ;;
	*)
        INPUTFILE="$INPUTFILE $1"
        ;;
	esac
	shift
done

if [[ -z "$OUTPUTFILE" ]] ; then
    OUTPUTFILE=output-$(date +%s).pdf
fi

if [[ -z "$INPUTFILE" ]] ; then
   printf "%s" "$USAGE"
   exit 1
fi

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE="$OUTPUTFILE" -dBATCH $INPUTFILE
